package services

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/pkg/errors"
	"weiboxs.com/new_h_glasses/models"
)

type ErpInventoryService struct {
}

//查询单条库存信息
func (s *ErpInventoryService) QueryInventoryOne(inventory_id int64, hascode string) *models.ErpInventory {

	inventory := new(models.ErpInventory)
	query := orm.NewOrm().QueryTable(models.ErpInventoryTableName())
	if inventory_id > 0 {
		query = query.Filter("id", inventory_id)
	}
	if len(hascode) > 0 {
		query = query.Filter("Hascode", hascode)
	}
	query.One(inventory)
	return inventory
}

//新增库存
func (s *ErpInventoryService) InsertErpInventory(o orm.TxOrmer, inv *models.ErpInventory) error {
	if _, err := o.Insert(inv); err != nil {
		return err
	}
	return nil
}

//更新库存
func (s *ErpInventoryService) UpdateErpInventory(o orm.TxOrmer, inv *models.ErpInventory, number int64) error {
	if inv.Id == 0 {
		return errors.New("库存参数错误")
	}
	inv.Inventory += number
	if _, err := o.Update(inv, "inventory"); err != nil {
		return err
	}
	return nil
}

//验证库存是否存在
func CheckExistInventory(has string) *models.ErpInventory {
	inv := models.ErpInventory{}
	orm.NewOrm().QueryTable(models.ErpInventoryTableName()).Filter("hascode", has).One(&inv)
	return &inv
}

//重新计算哈希值
func ReCalculationHascode() error {
	inventorymodels := make([]*models.ErpInventory, 0)
	to,_ := orm.NewOrm().Begin()
	to.QueryTable(models.ErpInventoryTableName()).Limit(-1).All(&inventorymodels)

	if len(inventorymodels) <= 0 {
		return errors.New("没有库存记录")
	}

	for k, _ := range inventorymodels {
		hascode, err := models.CalculateInventoryHascode(inventorymodels[k])
		if err != nil {
			to.Rollback()
			return err
		}
		inventorymodels[k].Hascode = hascode
		if _, err := to.Update(inventorymodels[k], "hascode"); err != nil {
			to.Rollback()
			return err
		}
	}
	to.Commit()
	return nil
}


//销减库存
func cutInventory(o orm.TxOrmer, purchaseno string, inv *models.ErpInventory, number int64) error {
	if (inv.Id == 0) {
		return errors.New("商品库存不足，请及时处理")
	}
	if (inv.Inventory - number) < 0 {
		return errors.New("商品库存不足，请及时处理")
	}
	invSer := new(ErpInventoryService)
	flowingData := make([]*models.ErpFlowingData, 0)

	//设置流水明细
	flowing := new(models.ErpFlowingData)
	flowing.Typeid = 7 //销售出库
	flowing.DepartmentId = inv.DepartmentId
	flowing.Products = inv.Products
	flowing.Acceptance = inv.Acceptance
	flowing.Snd = inv.Snd
	flowing.InventoryId = inv.Id
	flowing.PrimaryStorageNum = inv.Inventory
	flowing.StorageNum = number
	flowing.PurchaseUpNo = purchaseno
	flowingData = append(flowingData, flowing)
	//更新库存
	if err := invSer.UpdateErpInventory(o, inv, (0 - number)); err != nil {
		o.Rollback()
		return err
	}
	//写入流水明细
	flSer := new(ErpFlowingService)
	if err := flSer.SaveErpFlowingData(o, &models.ErpFlowing{Id: 0}, flowingData); err != nil {
		o.Rollback()
		return err
	}
	return nil
}

//销退库存
func addInventory(o orm.TxOrmer, purchaseno string, inv *models.ErpInventory, number int64) error {

	invSer := new(ErpInventoryService)
	flowingData := make([]*models.ErpFlowingData, 0)

	//设置流水明细
	flowing := new(models.ErpFlowingData)
	flowing.Typeid = 8 //销售出库
	flowing.DepartmentId = inv.DepartmentId
	flowing.Products = inv.Products
	flowing.Acceptance = inv.Acceptance
	flowing.Snd = inv.Snd
	flowing.InventoryId = inv.Id
	flowing.PrimaryStorageNum = inv.Inventory
	flowing.StorageNum = number
	flowing.PurchaseUpNo = purchaseno
	flowingData = append(flowingData, flowing)
	//更新库存
	if err := invSer.UpdateErpInventory(o, inv, number); err != nil {
		o.Rollback()
		return err
	}
	//写入流水明细
	flSer := new(ErpFlowingService)
	if err := flSer.SaveErpFlowingData(o, &models.ErpFlowing{Id: 0}, flowingData); err != nil {
		o.Rollback()
		return err
	}
	return nil
}